/*
 * @Description:共享路由
 * @Author: maskMan
 * @LastEditTime: 2025-03-11 18:13:50
 */
import Joint from '../mongo/joint'
import { getAnimateList, getDetectList, getTrainList, helpList } from '../public/gameApp'
import { resSend } from '../public/httpServe'
const express = require('express')
const router = express.Router()
//获取训练营列表
router.get('/getTrainCategory', async (req, res) => {
  try {
    const { type } = req.query
    const { account, id, loginCount } = req.auth
    if (!account || !id) {
      console.log('getTrainCategory非法请求', req.query, req.auth)
      return resSend(res, null, false, '非法请求')
    }
    if (!type) {
      console.log('getTrainCategory参数不正确', req.query)
      return resSend(res, null, false, '参数不正确')
    }
    const result = await Joint.getUserInfo(account, 'vipLevel', 'loginCount')
    if (!result || !result.vipLevel) {
      console.log('getTrainCategory非法请求', req.query, req.auth)
      return resSend(res, null, false, '非法请求')
    }
    if (loginCount != result.loginCount) {
      console.log('getTrainCategory用户重复登录', req.body)
      return resSend(res, { offLine: true }, false, '账号在其他地方已登陆')
    }
    const retData = getTrainList(result.vipLevel, type)
    if (!retData) {
      console.log('getTrainCategory获取失败', req.query, req.auth)
      return resSend(res, null, false, '获取失败')
    }
    return resSend(res, retData, true, '获取成功')
  } catch (error) {
    console.error('userAuth系统错误', req, error)
    return resSend(res, null, false, '系统错误')
  }
})
//获取检测项目列表
router.get('/getDetectCategory', async (req, res) => {
  try {
    const { type } = req.query
    const { account, id, loginCount } = req.auth
    if (!account || !id) {
      console.log('getTrainCategory非法请求', req.query, req.auth)
      return resSend(res, null, false, '非法请求')
    }
    if (!type) {
      console.log('getTrainCategory参数不正确', req.query)
      return resSend(res, null, false, '参数不正确')
    }
    const result = await Joint.getUserInfo(account, 'vipLevel', 'loginCount')
    if (!result || !result.vipLevel) {
      console.log('getTrainCategory非法请求', req.query, req.auth)
      return resSend(res, null, false, '非法请求')
    }
    if (loginCount != result.loginCount) {
      console.log('getDetectCategory用户重复登录', req.body)
      return resSend(res, { offLine: true }, false, '账号在其他地方已登陆')
    }
    const retData = getDetectList(type, result.vipLevel)
    if (!retData) {
      console.log('getTrainCategory获取失败', req.query, req.auth)
      return resSend(res, null, false, '获取失败')
    }
    return resSend(res, retData, true, '获取成功')
  } catch (error) {
    console.error('userAuth系统错误', req, error)
    return resSend(res, null, false, '系统错误')
  }
})
//获取动画项目列表
router.get('/getAnimateCategory', async (req, res) => {
  try {
    const { type } = req.query
    const { account, id, loginCount } = req.auth
    if (!account || !id) {
      console.log('getTrainCategory非法请求', req.query, req.auth)
      return resSend(res, null, false, '非法请求')
    }
    if (!type) {
      console.log('getTrainCategory参数不正确', req.query)
      return resSend(res, null, false, '参数不正确')
    }
    const result = await Joint.getUserInfo(account, 'vipLevel', 'loginCount')
    if (!result || !result.vipLevel) {
      console.log('getTrainCategory非法请求', req.query, req.auth)
      return resSend(res, null, false, '非法请求')
    }
    if (loginCount != result.loginCount) {
      console.log('getAnimateCategory用户重复登录', req.body)
      return resSend(res, { offLine: true }, false, '账号在其他地方已登陆')
    }
    const retData = getAnimateList(result.vipLevel, type)
    if (!retData) {
      console.log('getTrainCategory获取失败', req.query, req.auth)
      return resSend(res, null, false, '获取失败')
    }
    return resSend(res, retData, true, '获取成功')
  } catch (error) {
    console.error('userAuth系统错误', req, error)
    return resSend(res, null, false, '系统错误')
  }
})
//获取更新二维码
router.get('/getHelpList', async (req, res) => {
  try {
    const { account, id } = req.auth
    if (!account || !id) {
      console.log('getHelpList非法请求', req.query, req.auth)
      return resSend(res, null, false, '非法请求')
    }
    return resSend(res, helpList, true, '获取成功')
  } catch (error) {
    console.error('getHelpList系统错误', req, error)
    return resSend(res, null, false, '系统错误')
  }
})
//获取商品
router.get('/getShops', async (req, res) => {
  try {
    const { account, id } = req.auth
    if (!account || !id) {
      console.log('getShops非法请求', req.query, req.auth)
      return resSend(res, null, false, '非法请求')
    }
    const { typeName } = req.query
    if (!typeName) {
      console.log('getShops参数不正确', req.query, req.auth)
      return resSend(res, null, false, '非法请求')
    }
    const result = await Joint.getShops(typeName)
    if (!result) {
      console.log('getShops列表为空', req.query, req.auth)
      return resSend(res, null, true, '非法请求')
    }
    for (let index = 0; index < result.length; index++) {
      result[index]['sid'] = result[index].SID
      delete result[index].SID
      delete result[index]._id
    }
    return resSend(res, result, true, '获取成功')
  } catch (error) {
    console.error('getShops系统错误', req, error)
    return resSend(res, null, false, '系统错误')
  }
})
export default router
